package zyh.hibernate.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import java.util.List;

public class NewsManager {
    public static void main(String[] args) {
        Configuration conf = new Configuration().configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(conf.getProperties()).build();
        SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
        Session sess = sf.openSession();
        Transaction tx = sess.beginTransaction();
        String hql="select n.user from News n where n.id=1";
        List l=sess.createQuery(hql).list();
        Object o=l.get(0);
        System.out.println(o.toString());
        System.out.println(l);
        tx.commit();
        // 关闭Session
        sess.close();
        sf.close();
    }
}
